package model;


import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;


import excecao.ExceptionFinalArquivo;


public class Leitor {
	
	public static final char FIM_DE_ARQUIVO = '$';
	
	public static final int NUM_ESPACO = 32;

	public static final int NUM_TAB = 9;

	public static final int NUM_ENTER = 10;
	
	public BufferedReader lerArquivo(String nome) throws IOException {
		FileReader arquivo = new FileReader(nome);
		BufferedReader br = new BufferedReader(arquivo);
		return br;
		
	}
	

	public static char leiaCaractere(BufferedReader bf) throws IOException {
		int num = bf.read();
		if(Character.valueOf('\n') ==  num){
			AnalisadorLexico.contLinhas++;
		}
		char caractere;
		if (num >= 0) {
			char[] ch = Character.toChars(num);
			caractere = ch[0];
		} else {
			caractere = FIM_DE_ARQUIVO;
		}
		return caractere;
	}

	public static char leiaCaractereNaoBranco(BufferedReader bf) throws IOException,
			ExceptionFinalArquivo {
		
		int num = bf.read();
		if(Character.valueOf('\n') ==  num){
			AnalisadorLexico.contLinhas++;
		}
		if (num < 0) {
			throw new ExceptionFinalArquivo();
		}
		char[] ch = Character.toChars(num);
		char caractere = ch[0];
		boolean encontrouCaractere = false;
		while (num >= 0 && !encontrouCaractere) {
			if (!CaractereBranco(num)) {
				encontrouCaractere = true;
				caractere = ch[0];
			} else {
				num = bf.read();
				if (num >= 0) {
					caractere = ch[0];
					ch = Character.toChars(num);
				}
			}
			
		}
		if (num < 0) {
			caractere = FIM_DE_ARQUIVO;
		}
		

		return caractere;
	}

	public static boolean CaractereBranco(int valor) {
		
		if (valor == NUM_ENTER || valor == NUM_ESPACO || valor == NUM_TAB) {
			return true;
		} else {
			return false;
		}
	}


}
